package gui;

import gui.dialogs.dialogPanels;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

import dataTypes.*;

public class TablaGenerator extends JPanel {
	static String tabnev;
	public static int id;

	public TablaGenerator(JFrame frame, String tabnev, int id) {
		super(new BorderLayout());
		this.tabnev = tabnev;
		this.id = id;
		add(new JScrollPane(makeTable()));
		//setBorder(BorderFactory
			//	.createTitledBorder("Multiple Buttons in a Table Cell"));
		//setPreferredSize(new Dimension(300, 240));
		
	}

	private JTable makeTable() {
		
		String[] columnNames = null;
		Object[][] data = null;
		int columns = 0;
		if (tabnev.equals("film")) {
			String[] columnNames2 = { "ID", "Cim", "Lehetőségek" }; 
			
			DBConnect dbc = new DBConnect(); 
			Vector<IntStr> cimek = dbc.getFilmCimek();
			Object[][] data2 = convertArrayFilmkezelo(cimek);
			
			columnNames = columnNames2;
			data = data2;
			setBorder(BorderFactory.createTitledBorder(" Filmek "));
			columns = 2;
		} else if (tabnev.equals("komment")) {
			String[] columnNames2 = { "ID", "Felh", "Dátum", "Szoveg", "Lehetőségek" }; 
			
			DBConnect dbc = new DBConnect(); 
			Vector<Komment> cimek = dbc.getKommentek(id, 1);
			Object[][] data2 = convertArrayKomment(cimek);
			
			columnNames = columnNames2;
			data = data2;
			setBorder(BorderFactory.createTitledBorder(" Kommentek "));
			columns = 4;
		} else if (tabnev.equals("admin")) {
			//id=0 Cikkek
			//id=1 Felhasznalok
			//id=2 Jegytipusok
			//id=3 Termek
			DBConnect dbc = new DBConnect(); 
			switch(id) {
				case 0: {
					String[] columnNames2 = { "id", "Cim", "Felado", "feladasdatum", "Bevezeto", "Szoveg", "Lehetőségek" }; 
					Vector<Cikk> cikk = dbc.getCikkek();
					Object[][] data2 = convertArrayAdminCikk(cikk);
					
					columnNames = columnNames2;
					data = data2;
					setBorder(BorderFactory.createTitledBorder(" Cikkek "));
					columns = 6;
					break;
				}
				case 1: {
					String[] columnNames2 = { "ID", "Felhnev", "Vnev", "Knev", "Pw", "E-mail", "Utolsobej.", "Jog.", "Lehetőségek" }; 
					Vector<Felhasznalo> felhasznalo = dbc.getFelhasznalok();
					Object[][] data2 = convertArrayAdminFelh(felhasznalo);
					columnNames = columnNames2;
					data = data2;
					setBorder(BorderFactory.createTitledBorder(" Felhasználók "));
					columns = 8;
					break;
				}
				case 2: {
					String[] columnNames2 = { "ID", "Nev", "Kedv. mert.", "Lehetőségek" }; 
					Vector<Jegytipus> jtip = dbc.getJegytipusok();
					Object[][] data2 = convertArrayAdminJegytipus(jtip);
					System.out.println("case2");
					columnNames = columnNames2;
					data = data2;
					setBorder(BorderFactory.createTitledBorder(" Jegytípusok "));
					columns = 3;
					break;
				}
				case 3: {
					String[] columnNames2 = { "ID", "Nev", "Leiras", "Lehetőségek" }; 
					System.out.println("case3");
					Vector<Terem> t = dbc.getTermek();
					Object[][] data2 = convertArrayAdminTerem(t);
					
					columnNames = columnNames2;
					data = data2;
					setBorder(BorderFactory.createTitledBorder(" Jegytípusok "));
					columns = 3;
					break;
				}
			}
			
		} else {
		
			String[] columnNames2 = { "String", "Button" };
			Object[][] data2 = { { "AAA", "" }, { "CCC", "" }, { "BBB", "" }, { "ZZZ", "" } };
			columnNames = columnNames2;
			data = data2;
			columns = 1;
			setBorder(BorderFactory.createTitledBorder(" Multiple Buttons in a Table Cell "));
		}
		
		
		 
		DefaultTableModel model = new DefaultTableModel(data, columnNames) {
			
			@Override
			public Class<?> getColumnClass(int column) {
				
				return getValueAt(0, column).getClass();
			}
		};
		final JTable table = new JTable(model);
		//JScrollBar hbar = new JScrollBar(JScrollBar.HORIZONTAL, 30, 20, 0, 300);
	    //JScrollBar vbar = new JScrollBar(JScrollBar.VERTICAL, 30, 40, 0, 300);

	    //hbar.setUnitIncrement(2);
	    //hbar.setBlockIncrement(1);

	    //add(hbar, BorderLayout.SOUTH);
	    //add(vbar, BorderLayout.EAST);
		
		
		table.setRowHeight(36);
		//table.setSize(new Dimension(300, 240));
		
		table.setAutoCreateRowSorter(true);
		// table.addMouseListener(new CellButtonsMouseListener());
		// ButtonsEditorRenderer er = new ButtonsEditorRenderer(table);
		TableColumn column = table.getColumnModel().getColumn(columns);
		column.setCellRenderer(new ButtonsRenderer());
		column.setCellEditor(new ButtonsEditor(table));
		column.setMinWidth(165);
		TableColumn column0 = table.getColumnModel().getColumn(0);
		column0.setMaxWidth(10);
		return table;
	}

public static Object[][] convertArrayKomment(Vector<Komment> kat){
	Object[][] con = new Object[kat.size()][5];
	DBConnect dbc = new DBConnect();
	for(int i=0; i<kat.size(); i++){			
		con[i][0] = kat.elementAt(i).id;
		con[i][1] = dbc.getFelhasznalonev(kat.elementAt(i).felhid);
		con[i][2] = kat.elementAt(i).datum;
		con[i][3] = kat.elementAt(i).szoveg;
		con[i][4] = "";
	}

	return con;

}

public static Object[][] convertArrayFilmkezelo(Vector<IntStr> kat){
	Object[][] con = new Object[kat.size()][3];
	DBConnect dbc = new DBConnect();
	for(int i=0; i<kat.size(); i++){			
		con[i][0] = kat.elementAt(i).getId();
		con[i][1] = kat.elementAt(i).getAdat();
		con[i][2] = "";
	}

	return con;

}

public static Object[][] convertArrayAdminFelh(Vector<Felhasznalo> kat){
	Object[][] con = new Object[kat.size()][9];
	DBConnect dbc = new DBConnect();
	for(int i=0; i<kat.size(); i++){			
		con[i][0] = kat.elementAt(i).ID;
		con[i][1] = kat.elementAt(i).felhnev;
		con[i][2] = kat.elementAt(i).vnev;
		con[i][3] = kat.elementAt(i).knev;
		con[i][4] = kat.elementAt(i).jelszo;
		con[i][5] = kat.elementAt(i).email;
		con[i][6] = kat.elementAt(i).utolsobej;
		con[i][7] = kat.elementAt(i).jogosultsag;
		con[i][8] = "";
	}

	return con;

}

public static Object[][] convertArrayAdminCikk(Vector<Cikk> kat){
	Object[][] con = new Object[kat.size()][7];
	DBConnect dbc = new DBConnect(); 
	
	for(int i=0; i<kat.size(); i++){			
		con[i][0] = kat.elementAt(i).ID;
		con[i][1] = kat.elementAt(i).Cikk_cime;
		con[i][2] = dbc.getFelhasznalonev(kat.elementAt(i).FeladoID);
		con[i][3] = kat.elementAt(i).FeladasDatuma;
		con[i][4] = kat.elementAt(i).Bevezeto;
		con[i][5] = kat.elementAt(i).Szoveg;
		con[i][6] = "";
	}

	return con;

}

public static Object[][] convertArrayAdminTerem(Vector<Terem> kat){
	Object[][] con = new Object[kat.size()][4];
	
	for(int i=0; i<kat.size(); i++){			
		con[i][0] = kat.elementAt(i).Teremid;
		con[i][1] = kat.elementAt(i).teremnev;
		con[i][2] = kat.elementAt(i).teremleiras;
		con[i][3] = "";
	}

	return con;

}

public static Object[][] convertArrayAdminJegytipus(Vector<Jegytipus> kat){
	Object[][] con = new Object[kat.size()][4];
	
	for(int i=0; i<kat.size(); i++){			
		con[i][0] = kat.elementAt(i).id;
		con[i][1] = kat.elementAt(i).jegytipusnev;
		con[i][2] = kat.elementAt(i).kedvezmenymertek;
		con[i][3] = "";
	}

	return con;

}

}
// class CellButtonsMouseListener extends MouseAdapter{
// @Override public void mouseReleased(MouseEvent e) {
// JTable t = (JTable)e.getComponent();
// Point pt = e.getPoint();
// int row = t.rowAtPoint(pt);
// int col = t.columnAtPoint(pt);
// if(t.convertRowIndexToModel(row)>=0 && t.convertColumnIndexToModel(col)==1) {
// TableCellEditor ce = t.getCellEditor(row, col);
// ce.stopCellEditing();
// Component c = ce.getTableCellEditorComponent(t, null, true, row, col);
// Point p = SwingUtilities.convertPoint(t, pt, c);
// Component b = SwingUtilities.getDeepestComponentAt(c, p.x, p.y);
// if(b instanceof JButton) ((JButton)b).doClick();
// }
// }
// }

class ButtonsPanel extends JPanel {
	public final java.util.List<JButton> buttons = java.util.Arrays.asList(
			new JButton("Módosít"), new JButton("Töröl"));

	public ButtonsPanel() {
		super();
		setOpaque(true);
		for (JButton b : buttons) {
			b.setFocusable(false);
			b.setRolloverEnabled(false);
			add(b);
		}
	}
	// @Override public void updateUI() {
	// super.updateUI();
	// }
}

class ButtonsRenderer extends ButtonsPanel implements TableCellRenderer {
	public ButtonsRenderer() {
		super();
		setName("Table.cellRenderer");
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		this.setBackground(isSelected ? table.getSelectionBackground() : table
				.getBackground());
		return this;
	}
}

class ButtonsEditor extends ButtonsPanel implements TableCellEditor {
	public ButtonsEditor(final JTable table) {
		super();
		MouseListener ml = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				ButtonModel m = ((JButton) e.getSource()).getModel();
				if (m.isPressed() && table.isRowSelected(table.getEditingRow())
						&& !e.isShiftDown()) {
					setBackground(table.getBackground());
				}
			}
		};

		buttons.get(0).addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// JOptionPane.showMessageDialog(table, "Viewing");
				int row2 = table.convertRowIndexToModel(table.getEditingRow());
				Object o = table.getModel().getValueAt(row2, 0);
				int row = Integer.parseInt(o.toString());
				fireEditingStopped();
				if (TablaGenerator.tabnev.equals("komment")) {
					dialogPanels.CreateKommentModositoDialog(row);
				} else if (TablaGenerator.tabnev.equals("admin")) {
					switch(TablaGenerator.id) {
						case 0: {
							dialogPanels.CreateCikkModositoDialog(row);
							break;
						}
						case 1: {
							dialogPanels.CreateFelhasznaloModositoDialog(row);
							break;
						}
						case 2: {
							dialogPanels.CreateJegytipusModositoDialog(row);
							break;
						}
						case 3: {
							dialogPanels.CreateTeremModositoDialog(row);
							break;
						}
					}
					
				} else if (TablaGenerator.tabnev.equals("film")) {
					dialogPanels.CreateFilmModositoDialog(row);
				} 
			}
		});
		buttons.get(0).addMouseListener(ml);

		buttons.get(1).addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// Object o =
				// table.getModel().getValueAt(table.getSelectedRow(), 0);
				int row2 = table.convertRowIndexToModel(table.getEditingRow());
				Object o = table.getModel().getValueAt(row2, 0);
				int row = Integer.parseInt(o.toString());
				fireEditingStopped();
				dialogPanels.CreateTorlesDialog(TablaGenerator.tabnev, row, TablaGenerator.id);
			}
		});
		buttons.get(1).addMouseListener(ml);

		addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				fireEditingStopped();
			}
		});
	}

	@Override
	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected, int row, int column) {
		this.setBackground(table.getSelectionBackground());
		return this;
	}

	@Override
	public Object getCellEditorValue() {
		return "";
	}

	// Copid from AbstractCellEditor
	// protected EventListenerList listenerList = new EventListenerList();
	transient protected ChangeEvent changeEvent = null;

	@Override
	public boolean isCellEditable(java.util.EventObject e) {
		return true;
	}

	@Override
	public boolean shouldSelectCell(java.util.EventObject anEvent) {
		return true;
	}

	@Override
	public boolean stopCellEditing() {
		fireEditingStopped();
		return true;
	}

	@Override
	public void cancelCellEditing() {
		fireEditingCanceled();
	}

	@Override
	public void addCellEditorListener(CellEditorListener l) {
		listenerList.add(CellEditorListener.class, l);
	}

	@Override
	public void removeCellEditorListener(CellEditorListener l) {
		listenerList.remove(CellEditorListener.class, l);
	}

	public CellEditorListener[] getCellEditorListeners() {
		return (CellEditorListener[]) listenerList
				.getListeners(CellEditorListener.class);
	}

	protected void fireEditingStopped() {
		// Guaranteed to return a non-null array
		Object[] listeners = listenerList.getListenerList();
		// Process the listeners last to first, notifying
		// those that are interested in this event
		for (int i = listeners.length - 2; i >= 0; i -= 2) {
			if (listeners[i] == CellEditorListener.class) {
				// Lazily create the event:
				if (changeEvent == null)
					changeEvent = new ChangeEvent(this);
				((CellEditorListener) listeners[i + 1])
						.editingStopped(changeEvent);
			}
		}
	}

	protected void fireEditingCanceled() {
		// Guaranteed to return a non-null array
		Object[] listeners = listenerList.getListenerList();
		// Process the listeners last to first, notifying
		// those that are interested in this event
		for (int i = listeners.length - 2; i >= 0; i -= 2) {
			if (listeners[i] == CellEditorListener.class) {
				// Lazily create the event:
				if (changeEvent == null)
					changeEvent = new ChangeEvent(this);
				((CellEditorListener) listeners[i + 1])
						.editingCanceled(changeEvent);
			}
		}
	}
}
